package com.wskj.shiro.chapter2;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.util.Factory;
import org.apache.shiro.util.ThreadContext;
import org.junit.After;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * 测试用户登录与退出
 * @author Administrator
 *
 */
public class LoginLoginOutTest {

	@Ignore
	public  void  loginHello() {
		
		//1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
        Factory<SecurityManager> factory =
                new IniSecurityManagerFactory("classpath:shiro.ini");
        
        //2.得到SecurityManager实例 并绑定给SecurityUtils
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        
      //3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
		
		//4.
        try {
             subject.login(token);
             System.out.println("登录成功！~");
        }catch (AuthenticationException e) {
            //5、身份验证失败
            System.out.println("登录失败！");
		}
        
        // 判断是否登录成功
        Assert.assertEquals(true, subject.isAuthenticated()); 

        //6、退出
        subject.logout();
	}
	
	
	
	// 测试 登录的域  进行验证
	@Ignore
	public  void  testRealm() {
		//1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
        Factory<SecurityManager> factory =
                new IniSecurityManagerFactory("classpath:shiro-realm.ini");

        //2、得到SecurityManager实例 并绑定给SecurityUtils
        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
        Subject subject = SecurityUtils.getSubject();
        
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");

        try {
            //4、登录，即身份验证
            subject.login(token);
        } catch (AuthenticationException e) {
            //5、身份验证失败
            e.printStackTrace();
        }
        Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录		
	}
	
	@Ignore
	public  void  testMultiRealm() {
		Factory<SecurityManager> factory=new  IniSecurityManagerFactory("classpath:shiro-multi-realm.ini");
		
		SecurityManager securityManager = factory.getInstance();
		
		// 设置  验证管理
		SecurityUtils.setSecurityManager(securityManager);
		
		
		Subject subject = SecurityUtils.getSubject();
		
		
		UsernamePasswordToken token=new UsernamePasswordToken("lisi","456");
		
		try {
			
		    subject.login(token);
		    System.out.println("登录成功！");
		
		}catch(AuthenticationException e) {

			e.printStackTrace();
			
		}
         
		Assert.assertEquals(true, subject.isAuthenticated());
		subject.logout();
		
		
	}
	
	
	    @Test
	    public void testJDBCRealm() {
	        //1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
	        Factory<SecurityManager> factory =
	                new IniSecurityManagerFactory("classpath:shiro-jdbc-realm.ini");

	        //2、得到SecurityManager实例 并绑定给SecurityUtils
	        SecurityManager securityManager = factory.getInstance();
	        SecurityUtils.setSecurityManager(securityManager);

	        //3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
	        Subject subject = SecurityUtils.getSubject();
	        UsernamePasswordToken token = new UsernamePasswordToken("lisi", "456");

	        try {
	            //4、登录，即身份验证
	            subject.login(token);
	        } catch (AuthenticationException e) {
	            //5、身份验证失败
	            e.printStackTrace();
	        }

	        Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录

	        //6、退出
	        subject.logout();
	    }

	    
	    
	    @After
	    public void tearDown() throws Exception {
	        ThreadContext.unbindSubject();//退出时请解除绑定Subject到线程 否则对下次测试造成影响
	    }
	
	
}
